﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace WaterRegimenMonitor.Web.Domain
{
    /// <summary>
    /// 用户认证领域类。
    /// </summary>
    public class Authentication
    {
        /// <summary>
        /// 用户登陆。
        /// </summary>
        /// <param name="userID">用户名。</param>
        /// <param name="response">HTTP反馈对象。</param>
        public static void Login(string userID, HttpResponse response)
        {
            //获得超时时间
            DateTime expiration = DateTime.Now.AddDays(1);

            //创建窗体认证凭据
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userID, DateTime.Now, expiration, false, string.Empty);

            //创建认证Cookie
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));

            //添加认证Cookie
            response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 登出。
        /// </summary>
        public static void Logout()
        {
            //执行登出操作
            FormsAuthentication.SignOut();
        }
    }
}
